package com.software.controller;


import com.software.base.ResultInfo;
import com.software.bean.CustomerAddressBean;
import com.software.dto.AddAddressDto;
import com.software.dto.UpdateAddressDto;
import com.software.service.CustomerAddressService;
import com.software.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wyj
 * @since 2024-03-31
 */
@Controller
@RequestMapping("/customerAddress")
public class CustomerAddressController {

    @Autowired
    private CustomerAddressService customerAddressService;

    @PostMapping("/front/addAddress")
    @ResponseBody
    public ResultInfo addAddress(@RequestBody AddAddressDto addAddressDto) {
        Integer result = customerAddressService.addAddress(addAddressDto);
        AssertUtil.isTrue(result != 1, "添加失败");
        return ResultInfo.success();
    }

    @GetMapping("/front/addressList")
    @ResponseBody
    public ResultInfo addressList(@RequestParam Integer cid) {
        List<CustomerAddressBean> list = customerAddressService.addressList(cid);
        return ResultInfo.success(list);
    }

    @GetMapping("/front/getAddressByAddressId/{aId}")
    @ResponseBody
    public ResultInfo getAddressByAddressId(@PathVariable Integer aId) {
        CustomerAddressBean addressBean = customerAddressService.getAddressByAddressId(aId);
        return ResultInfo.success(addressBean);
    }

    @PostMapping("/front/updateAddress")
    @ResponseBody
    public ResultInfo updateAddress(@RequestBody UpdateAddressDto updateAddressDto) {
         Integer result = customerAddressService.updateAddress(updateAddressDto);
        AssertUtil.isTrue(result != 1, "更新失败");
        return ResultInfo.success();
    }

    @DeleteMapping("/front/deleteAddressByAddressId/{addressId}")
    @ResponseBody
    public ResultInfo deleteAddressById(@PathVariable Integer addressId) {
        Integer result = customerAddressService.deleteAddressById(addressId);
        AssertUtil.isTrue(result != 1, "删除失败");
        return ResultInfo.success();
    }
}

